package org.yunai.swjg.client.mina;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: yunai
 * Date: 13-1-30
 * Time: 下午2:38
 */
public class ClientIoHandler implements IoHandler {

    private AtomicInteger messageCount = new AtomicInteger();

//    private CommandManager commandManager = new CommandManager();

    public ClientIoHandler() {
//        commandManager.addCommand(CommandManager.TYPE_MESSAGE, C_S_LoginReq.CODE, new UserLoginCommand());
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("连接成功");
        //To change body of implemented methods use File | Settings | File Templates.

//        C_S_LoginReq loginReq = new C_S_LoginReq("骚球");
//        MessageWrapper messageWrap = new MessageWrapper(C_S_LoginReq.CODE, loginReq);
//        session.write(messageWrap);
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        System.out.println("一条消息正在涌入(" + messageCount.incrementAndGet() + ")");
        System.out.println("消息内容：" + message);

//        ProtobufMessage request = (ProtobufMessage) message;
//        ProtobufMessage response = CommandUtil.get(request.getVocation()).execute(request.getBody());
//        MessageWrapper messageWrap = (MessageWrapper) message;
//        commandManager.execute(CommandManager.TYPE_MESSAGE, messageWrap.getVocation(), session, message);

//        System.out.println("一条消息正在涌出(" + messageCount.get() + ")");
//        session.write(response);
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
